package com.gzcc.utils;

import com.alibaba.fastjson.JSONObject;
import com.gzcc.core.wechat.AccessToken;
import com.gzcc.core.wechat.Button;
import com.gzcc.core.wechat.Menu;
import com.gzcc.core.wechat.ViewButton;


import org.apache.http.HttpEntity;
import org.apache.http.ParseException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


import java.io.IOException;

/**
 * @Description: 微信公众号菜单工具类
 * @Author: Mr.Zhong
 * @Date: 2018/6/13
 */
public class MeunUtil {
    private static final String APPID = "wx7646ca6bb92fa339";
    private static final String APPSECRET = "2584e088977867de01b1015def0ea45d";
    private static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
    private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";

    /**
     * @Description: get请求获取JSONObject
     * @Param: 请求路径，url
     * @return: JSONObject
     * @Author: Mr.Zhong
     * @Date: 2018/6/13
     */
    public static JSONObject doGetStr(String url) throws ParseException, IOException {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        JSONObject jsonObject = null;
        HttpResponse httpResponse = client.execute(httpGet);
        HttpEntity entity = httpResponse.getEntity();
        if (entity != null) {
            String result = EntityUtils.toString(entity, "UTF-8");
            jsonObject = JSONObject.parseObject(result);
        }
        return jsonObject;
    }

    /**
     * @Description: post请求获取JSONObject
     * @Param: 请求路径，url，post请求参数 outStr
     * @return: JSONObject
     * @Author: Mr.Zhong
     * @Date: 2018/6/13
     */
    public static JSONObject doPostStr(String url, String outStr) throws ParseException, IOException {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost httpost = new HttpPost(url);
        JSONObject jsonObject = null;
        httpost.setEntity(new StringEntity(outStr, "UTF-8"));
        HttpResponse response = client.execute(httpost);
        String result = EntityUtils.toString(response.getEntity(), "UTF-8");
        jsonObject = JSONObject.parseObject(result);
        return jsonObject;
    }

    /**
    * @Description: 获取微信公众号的token
    * @return:  AccessToken
    * @Author: Mr.Zhong
    * @Date: 2018/6/13
    */
    public static AccessToken getAccessToken() throws ParseException, IOException {
        AccessToken token = new AccessToken();
        String url = ACCESS_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET);
        JSONObject jsonObject = doGetStr(url);
        if (jsonObject != null) {
            token.setToken(jsonObject.getString("access_token"));
            token.setExpiresIn(jsonObject.getIntValue("expires_in"));
        }
        return token;
    }

    /**
    * 描述: 初始化菜单
    * @Author: Mr.Zhong
    * @Date: 2018/6/13
    */
    public static Menu initMenu() {
        Menu menu = new Menu();
        ViewButton button21 = new ViewButton();
        button21.setName("商城入口");
        button21.setType("view");
        button21.setUrl("http://test.wanzhihekeji.com/user");
        menu.setButton(new Button[]{button21});
        return menu;

    }
    /**
    * 描述: 创建菜单
    * @param   token
    * @param   menu
    * @Author: Mr.Zhong
    * @Date: 2018/6/13
    */
    public static int createMenu(String token, String menu) throws ParseException, IOException {
        int result = 0;
        String url = CREATE_MENU_URL.replace("ACCESS_TOKEN", token);
        JSONObject jsonObject = doPostStr(url, menu);
        if (jsonObject != null) {
            result = jsonObject.getIntValue("errcode");
        }
        return result;
    }
}

